TARGET = jinyong-legend

VITA_TITLE_NAME  = 金庸群侠传
VITA_TITLE_ID    = JYSDLLUA1
VITA_APP_VER     = 01.02
APP_VER          = 1.02

ROOT_DIR         := $(shell pwd)
SRC_DIR          := $(ROOT_DIR)/src
PKG_DIR          := $(ROOT_DIR)/pkg
OUT_DIR          := $(ROOT_DIR)/out

SRC_C := \
	$(SRC_DIR)/charset.c \
	$(SRC_DIR)/jymain.c \
	$(SRC_DIR)/luafun.c \
	$(SRC_DIR)/mainmap.c \
	$(SRC_DIR)/piccache.c \
	$(SRC_DIR)/sdlfun.c

OBJS  := $(SRC_C:.c=.o)
OBJS  := $(notdir $(OBJS))
OBJS  := $(addprefix $(OUT_DIR)/,$(OBJS))

PREFIX  := arm-vita-eabi
CC      := $(PREFIX)-gcc
CXX     := $(PREFIX)-g++
AS      := $(PREFIX)-as
AR      := $(PREFIX)-ar
OBJCOPY := $(PREFIX)-objcopy
STRIP   := $(PREFIX)-strip
NM      := $(PREFIX)-nm

DEFINES := -D__vita__

INCDIRS := \
	-I./lua-5.1.5/include
	
WARNINGS := \
	-Wall \
	-Wno-unused-variable \
	-Wno-unused-but-set-variable \
	-Wno-unused-function \
	-Wno-format-truncation

LIBDIRS := \
	-L./lua-5.1.5/lib \
	-L./SDL_mixer

LIBS := \
	-llua \
	-lSDL \
	-lSDL_image \
	-lSDL_mixer \
	-lSDL_ttf \
	-lfreetype \
	-lmikmod \
	-logg \
	-lwebp \
	-ljpeg \
	-lpng \
	-lz \
	-lvorbisfile \
	-lvorbis \
	-lpthread \
	-lSceDisplay_stub \
	-lSceCtrl_stub \
	-lSceAudio_stub \
	-lSceHid_stub

FLAGS = -Wl,-q -O3 $(WARNINGS) $(INCDIRS) $(DEFINES)
#FLAGS += -DHAS_SDL_MPEG=1
#FLAGS += -lsmpeg

CFLAGS = ${FLAGS}
CXXFLAGS = $(FLAGS) -std=c++11

EBOOT_BUILD_PATH := $(PKG_DIR)/eboot.bin
SFO_BUILD_PATH   := $(PKG_DIR)/sce_sys/param.sfo
VPK_BUILD_PATH := $(ROOT_DIR)/$(VITA_TITLE_NAME)_v$(APP_VER).vpk

all: dir_create build-liblua build-libSDL_mixer $(TARGET).vpk

build-liblua:
	cd lua-5.1.5 && make && make install

build-libSDL_mixer:
	cd SDL_mixer && make

clean-liblua:
	cd lua-5.1.5 && make clean
	rm -rf ./lua-5.1.5/lib ./lua-5.1.5/include ./lua-5.1.5/bin \
		./lua-5.1.5/man ./lua-5.1.5/share

clean-libSDL_mixer:
	cd SDL_mixer && make clean

$(TARGET).vpk: $(EBOOT_BUILD_PATH) $(SFO_BUILD_PATH)
	@rm -rf $(VPK_BUILD_PATH)
	cd $(PKG_DIR) && zip -r $(VPK_BUILD_PATH) ./*

eboot.bin: $(EBOOT_BUILD_PATH)

$(EBOOT_BUILD_PATH): $(OUT_DIR)/$(TARGET).velf
	vita-make-fself -c $< $@

$(SFO_BUILD_PATH):
	vita-mksfoex -s APP_VER="$(VITA_APP_VER)" -s TITLE_ID="$(VITA_TITLE_ID)" "$(VITA_TITLE_NAME)" $@

$(OUT_DIR)/$(TARGET).velf: $(OUT_DIR)/$(TARGET).elf
	$(STRIP) -g $<
	vita-elf-create $< $@

$(OUT_DIR)/${TARGET}.elf: ${OBJS}
	${CXX} ${CXXFLAGS} $(LIBDIRS) $^ $(LIBS) -o $@

$(OUT_DIR)/%.o: $(SRC_DIR)/%.cpp
	${CXX} -c ${CXXFLAGS} $< -o $@

$(OUT_DIR)/%.o: $(SRC_DIR)/%.c
	${CC} -c ${CFLAGS} $< -o $@

dir_create:
	$(shell if [ ! -e $(OUT_DIR) ];then mkdir -p $(OUT_DIR); fi)

clean:
	rm -rf $(OUT_DIR) ${VPK_BUILD_PATH} $(EBOOT_BUILD_PATH) $(SFO_BUILD_PATH)

clean-all: clean clean-liblua clean-libSDL_mixer

